package com.sgxy.aclservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sgxy.aclservice.entity.AclDept;
import com.sgxy.aclservice.entity.AclRole;
import com.sgxy.aclservice.entity.vo.AclDeptVo;
import com.sgxy.aclservice.entity.vo.OneDept;
import com.sgxy.aclservice.service.AclDeptService;
import com.sgxy.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-27
 */
@RestController
@RequestMapping("/aclservice/acldept")
public class AclDeptController {

    @Autowired
    private AclDeptService aclDeptService;
    //获取分页部门列表
    @PostMapping("{page}/{limit}")
    public R index(@PathVariable Long page,
                   @PathVariable Long limit,
                   AclDept aclDept){
        Page<AclDeptVo> deptPage = new Page<AclDeptVo>();
        //QueryWrapper<AclDept> wrapper = new QueryWrapper<>();
        //if(!StringUtils.isEmpty(aclDept.getDeptName())){
        //    wrapper.like("dept_name",aclDept.getDeptName());
        //}
        //aclDeptService.page(deptPage, wrapper);
        aclDeptService.selectPageVoList(deptPage,aclDept);
        return R.ok().data("items", deptPage.getRecords()).data("total", deptPage.getTotal());
    }
    //获取全部部门列表
    @GetMapping("getAllDeptTree")
    public R getAllDept(){
        List<OneDept> deptList = aclDeptService.getAllOneTwoDept();
        return R.ok().data("deptList",deptList);

    }
    @GetMapping("getAllDept")
    public R getRoleAll(){
        List<AclDept> list = aclDeptService.list(null);
        return R.ok().data("list",list);
    }

    //根据id获取部门
    @GetMapping("getDept/{id}")
    public R getDept(@PathVariable String id){
        AclDeptVo deptVo = aclDeptService.selectOneById(id);
        return R.ok().data("dept",deptVo);
    }
    //新增部门
    @PostMapping("addDept")
    public R addDept(@RequestBody AclDept aclDept){
        boolean flag = aclDeptService.save(aclDept);
        return flag?R.ok():R.error();
    }
    //修改
    @PostMapping("updateDept")
    public R updateDept(@RequestBody AclDept aclDept){
        boolean flag = aclDeptService.updateById(aclDept);
        return flag?R.ok():R.error();
    }
    //删除
    @DeleteMapping("{id}")
    public R deleteDeptById(@PathVariable String id){
        boolean flag = aclDeptService.deleteDeptById(id);
        return flag?R.ok():R.error();
    }

}

